package com.boco.hsqldb.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author chenjianxun
 * @version 1.0
 * @date 2021/6/24 21:11
 */
public class DateUtil {

    public static final SimpleDateFormat simpleDateFormat;

    static {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        simpleDateFormat = df;
    }


    public static String formatForDate(Date date) {
        if (date == null) {
            return "";
        } else {
            return simpleDateFormat.format(date);
        }
    }

    public static String formatForDate(Date date,String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); //设置格式
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        if (date == null) {
            return "";
        } else {
            return simpleDateFormat.format(date);
        }
    }

    public static Date getDateFromDateStr(String dateStr, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        assert date != null;
        return new Date(date.getTime());
    }

    /**
     * Description: 时间戳转Date</br>
     *
     * @param timestamp 时间戳 </br>
     */
    public static Date getDateFromTimestamp(Long timestamp) {
        if (timestamp == null) return null;
        if (timestamp.toString().length() == 10) {
            return new Date(timestamp * 1000);
        } else if (timestamp.toString().length() == 13) {
            return new Date(timestamp);
        } else {
            return null;
        }
    }

    /**
     * Description: 判断一个时间是否在一个时间段内 </br>
     *
     * @param nowTime   当前时间 </br>
     * @param beginTime 开始时间 </br>
     * @param endTime   结束时间 </br>
     */
    public static boolean isBetweenCalendars(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime 时间区间,双闭合,如10:00:00-20:00:00
     * @param curTime    需要判断的时间 如10:00:00
     * @return boolean
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        try {
            if (args[1].equals("0:00:00")) {
                args[1] = "24:00:00";
            }
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();

            if (end < start) {
                end = end + 24 * 3600 * 1000;
            }

            long start0 = start - 24 * 3600 * 1000;
            long end0 = end - 24 * 3600 * 1000;

            long start1 = start + 24 * 3600 * 1000;
            long end1 = end + 24 * 3600 * 1000;

            if (now >= start && now <= end) {
                return true;
            } else if (now >= start0 && now <= end0) {
                return true;
            } else return now >= start1 && now <= end1;
        } catch (ParseException e) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
    }


    /**
     * Description: 比较时间大小 </br>
     *
     * @param time1 时间1 </br>
     * @param time2   时间2 </br>
     */
    public static boolean isTime1BiggerTime2( Date time1, Date time2) {
        Calendar date1 = Calendar.getInstance();
        date1.setTime(time1);

        Calendar date2 = Calendar.getInstance();
        date2.setTime(time2);
        return date1.after(date2);
    }

}
